package scottb.cribbage.model {
import mx.events.PropertyChangeEvent;

[Bindable]
public class Hand {

    public var peggingPoints:PeggingPoints = new PeggingPoints();
    public var countingPoints:CountingPoints = new CountingPoints();
    public var cribPoints:CribPoints = null;
    public var hasCrib:Boolean = false;
    public var points:Points = new Points();
    
    public function Hand(crib:Boolean) {

        peggingPoints.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updatePoints);
        countingPoints.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updatePoints);

        if(crib) {

            hasCrib = true;
            cribPoints = new CribPoints();
            cribPoints.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updatePoints);
        }
    }
    
    private function updatePoints(event:PropertyChangeEvent):void {

        var points:int = peggingPoints.points + countingPoints.points;
        
        if(hasCrib) {
            points += cribPoints.points;
        }
        
        this.points.points = points;
    }
}
}
